package com.xy.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springblade.core.http.Exchange;
import org.springblade.core.http.HttpRequest;
import org.springblade.core.http.LogLevel;
import org.springblade.core.http.ResponseSpec;
import org.springblade.core.http.util.HttpUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;

import java.io.Serializable;
import java.rmi.ServerException;
import java.util.*;

/**
 * @BelongsPackage: com.xy.utils
 * @Author: WK
 * @CreateTime: 2023-07-28
 * @Description:
 * @Version: 1.0
 */
@Slf4j
public class SmsNotifiUtil {

	//发送签名
	private static final String SIGN_TEXT = "【天津直博会】";
	//密码
	private static final String PWD = "AT5M98RI9G";
	//用户账号
	private static final String CUST_CODE = "810432";

	private static final String URL = "https://smsapp.wlwx.com/sendSms";

	public static String send(List<String> phones, String content) {
		if (Func.isEmpty(phones)) {
			throw new ServiceException("电话号码不能为空");
		}
		if (Func.isEmpty(content)) {
			throw new ServiceException("发送的内容不能为空");
		}
		String msg = String.format("%s%s", SIGN_TEXT, content);
		String sign = sign(msg);
		Map<String, String> hands = new HashMap<>();
		hands.put("Content-Type", "application/json;charset=utf-8");
		SmsEntity sms = new SmsEntity();
		sms.setContent(msg);
		sms.setCust_code(CUST_CODE);
		sms.setSign(sign);
		sms.setDestMobiles(Func.join(phones));
		sms.setUuid(UUID.randomUUID().toString());
		sms.setNeed_report("no");
		log.info("请求短信接口参数数据:{}", JSONObject.toJSONString(sms));
		String res = HttpRequest.post(URL)
			.addHeader("Contentr-Type", "application/json;charset=utf-8").bodyString(JSONObject.toJSONString(sms))
			.execute()
			.onFailed((request, e) -> {
				e.printStackTrace();
			})
			.onSuccessful(ResponseSpec::asString);
		log.info("请求短信接口返回数据:{}", res);

		return res;
	}

	private static String sign(String content) {
		String msg = String.format("%s%s", content, PWD);
		return DigestUtils.md5Hex(msg);
	}

	@Data
	private static class SmsEntity {
		private String cust_code;
		private String sp_code;
		private String content;
		private String destMobiles;
		private String uuid;
		private String need_report;
		private String sign;

	}


}
